package com.yylending.remotevideo.anychat;

import com.bairuitech.anychat.AnyChatBaseEvent;
import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatDefine;
import com.bairuitech.anychat.AnyChatObjectDefine;
import com.yyfax.common.CLog;
import com.yylending.remotevideo.AnyChat;
import com.yylending.remotevideo.AnyChatPresenter;

/**
 * Created by HenryZhang on 2017/4/22.<br>
 * Copyright © 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class BaseEvent implements AnyChatBaseEvent {
	private static final String TAG = "BaseEvent";

	private AnyChatPresenter.View mView;
	private AnyChatCoreSDK anychat;

	private int SelfUserId;

	public BaseEvent(AnyChatPresenter.View view, AnyChatCoreSDK coreSdk) {
		mView = view;
		anychat = coreSdk;
	}

	public void release() {
		mView = null;
		anychat = null;
	}

	/**
	 * 连接服务器触发（connet），“bSuccess==true”连接服务器成功，反之连接服务器失败
	 */
	@Override
	public void OnAnyChatConnectMessage(boolean bSuccess) {
		CLog.i(TAG, "连接服务器成功=" + bSuccess);
		if (!bSuccess) {
			mView.loginResult(AnyChat.EVENT_CONNECT_SERVER_FAILED);
		}
	}

	/**
	 * 用户登录触发(login),dwUserId是服务器为客户端分配的唯一标识userid，dwErrorCode==0表示登录成功，其他值为登录服务器失败的错误代码
	 */
	@Override
	public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode) {
		CLog.i(TAG, "用户登录:userId=" + dwUserId + ", errorCode(0代表成功)=" + dwErrorCode + "（220表示签名时间失效）");
		if (dwErrorCode == 0) {//登录成功后，自动获取营业厅数据
			SelfUserId = dwUserId;

			//业务对象身份初始化；0代表普通客户，2是代表座席 (USER_TYPE_ID)
			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_OBJECT_INITFLAGS, 0);
			//业务对象优先级设定，默认为10
			AnyChatCoreSDK.ObjectSetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_CLIENTUSER, dwUserId, AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_PRIORITY, 10);
			//业务对象属性设定，默认为-1（表示具备所有业务技能）
//			AnyChatCoreSDK.ObjectSetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_CLIENTUSER, dwUserId, AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_ATTRIBUTE, -1);
			// 向服务器发送数据同步请求指令（同步已存在的营业厅数据，第二个参数为-1表示同步所有的营业厅）
			AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA, AnyChatObjectDefine.ANYCHAT_INVALID_OBJECT_ID,
					AnyChatObjectDefine.ANYCHAT_OBJECT_CTRL_SYNCDATA, dwUserId, 0, 0, 0, "");
			// 然后会收到业务对象更新事件ANYCHAT_OBJECT_EVENT_UPDATE（ObjectEvent.OnAnyChatObjectEvent()）

//			anychat.InitSDK(AnyChatDefine.BRAC_SO_OBJECT_INITFLAGS, AnyChatObjectDefine.ANYCHAT_OBJECT_FLAGS_CLIENT);//0普通用户，2坐席
//			//业务对象身份初始化；0代表普通客户，2是代表座席 (USER_TYPE_ID)
//			AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_OBJECT_INITFLAGS, AnyChatObjectDefine.ANYCHAT_OBJECT_FLAGS_CLIENT);
//			//业务对象优先级设定；
//			AnyChatCoreSDK.ObjectSetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_CLIENTUSER, dwUserId, AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_PRIORITY, 10);
//			//业务对象属性设定，关联技能分组，-1表示具备所有业务技能
//			int dwAttribute = -1;
//			AnyChatCoreSDK.ObjectSetIntValue(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_CLIENTUSER, dwUserId, AnyChatObjectDefine.ANYCHAT_OBJECT_INFO_ATTRIBUTE, dwAttribute);
//			// 向服务器发送数据同步请求指令
//			AnyChatCoreSDK.ObjectControl(AnyChatObjectDefine.ANYCHAT_OBJECT_TYPE_AREA, AnyChatObjectDefine.ANYCHAT_INVALID_OBJECT_ID, AnyChatObjectDefine.ANYCHAT_OBJECT_CTRL_SYNCDATA, dwUserId, 0, 0, 0, "");
		} else {
			mView.loginResult(AnyChat.EVENT_CONNECT_SERVER_FAILED);
		}
	}

	/**
	 * 当前房间用户离开或者进入房间触发这个回调，dwUserId用户 id," bEnter==true"表示进入房间,反之表示离开房间
	 */
	@Override
	public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter) {
		CLog.i(TAG, "用户离开或者进入房间：bEnter(true表示进入房间，反之表示离开房间) = " + bEnter);
	}

	/**
	 * 进入房间触发，dwRoomId为房间号，dwErrorCode==0表示进入房间成功，其他值为进入房间失败的错误代码
	 */
	@Override
	public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode) {
		CLog.i(TAG, "进入房间：RoomId = " + dwRoomId + "，errorCode(0代表成功) = " + dwErrorCode);
		if (dwErrorCode == 0) {
//			AnyChat.initMedia(anychat, mContext);
			mView.enterRoomResult(dwRoomId);
		} else {
			mView.loginResult(AnyChat.EVENT_ENTER_ROOM_FAILED);
		}
	}

	/**
	 * 当前房间在线用户消息，进入房间成功后调用一次。dwUserNum当前房间总人数（包含自己）
	 */
	@Override
	public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId) {
		CLog.i(TAG, "当前房间在线用户消息：RoomID = " + dwRoomId + "，房间总人数 = " + dwUserNum);
		mView.roomUserNum(dwUserNum, 0);
	}

	/**
	 * 跟服务器网络断触发该消息。收到该消息后可以关闭音视频以及做相关提示工作
	 */
	@Override
	public void OnAnyChatLinkCloseMessage(int dwErrorCode) {
		CLog.i(TAG, "视频云连接断开：errorCode = " + dwErrorCode);
		mView.loginResult(AnyChat.EVENT_CONNECT_BREAK_FAILED);
	}
}
